package com.contract.spirit.ui.exchange

import android.content.Context
import android.content.Intent
import com.contract.commonlib.base.BaseActivity
import com.contract.commonlib.http.ext.parseState
import com.contract.commonlib.util.RecycleviewLayoutManger
import com.contract.spirit.R
import com.contract.spirit.databinding.ActivityExchangeOrderSelectBinding
import com.contract.spirit.manage.BindExchangeDialogManager
import com.contract.spirit.ui.UserViewModel
import com.contract.spirit.ui.exchange.adapter.ExchangeOrderSelectAdapter
import com.contract.spirit.ui.order.MyOrderActivity

/**
 * @author: zhengjun
 * created: 2021/9/27
 * desc:
 */
class ExchangeOrderSelectActivity : BaseActivity<ActivityExchangeOrderSelectBinding,UserViewModel>() {
    private val exchangeOrderSelectAdapter by lazy {
        ExchangeOrderSelectAdapter()
    }
    override fun setContentView(): Int {
        return R.layout.activity_exchange_order_select
    }

    override fun initData() {
        RecycleviewLayoutManger.setVerticalManager(mBinding.rvExchange,mContext)
        mBinding.rvExchange.adapter = exchangeOrderSelectAdapter
        exchangeOrderSelectAdapter.setOnItemClickListener { _, _, position ->
            val item = exchangeOrderSelectAdapter.getItem(position)
            if (item.isHasBind()){
                MyOrderActivity.launch(mContext, item.exchangeType)
            }else{
                BindExchangeDialogManager.showBindExchangeDialog(mContext,item)
            }
        }
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        mViewModel.exchangeResponse.observe(this,{ it ->
            parseState(it,{
                exchangeOrderSelectAdapter.setList(it.exchangeList)
            })
        })
    }

    override fun onReload() {
        mViewModel.exchangeResponse.value = null
        getExchangeList()
    }

    private fun getExchangeList(){
        mViewModel.getExchangeList()
    }

    override fun onResume() {
        super.onResume()
        getExchangeList()
    }

    companion object{
        fun launch(mContext:Context){
            mContext.startActivity(
                Intent(
                    mContext,
                    ExchangeOrderSelectActivity::class.java
                )
            )
        }
    }
}